﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace CommLink
{
    class CommLinkDummyFileIO : CommLinkAbstract
    {
        BinaryWriter fWr;
        BinaryReader fRd;
        Thread msgReading;

        public override void closeConnection()
        {
            if (fWr != null)
            {
                fWr.Close();
                fWr = null;
            }

            if (fRd != null)
            {
                fRd.Close();
                fRd = null;
            }
        }

        public override void openConnection()
        {
            bool connectFailed = true;

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Communication files (*.cmm)|*.cmm|All files (*.*)|*.*";
            
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                fWr = new BinaryWriter((new FileInfo(sfd.FileName)).Create());
                connectFailed = false;

                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Communication files (*.cmm)|*.cmm|All files (*.*)|*.*";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    fRd = new BinaryReader((new FileInfo(ofd.FileName)).OpenRead());

                    msgReading = new Thread(new ThreadStart(MessageReceiveFire));
                    msgReading.IsBackground = true;
                    msgReading.Start();
                }
            }

            if (OnConnectionStatusChange != null)
            {
                if (connectFailed)
                    OnConnectionStatusChange(ConnectionStates.ServerError);
                else
                    OnConnectionStatusChange(ConnectionStates.Connected);
            }
        }

        public override int messageSend(byte[] OutData)
        {
            if (fWr != null)
            {
                fWr.Write(OutData.Length);
                fWr.Write(OutData);

                return 0;
            }

            return -1;
        }

        void MessageReceiveFire()
        {
            while (fRd != null && fRd.BaseStream.CanRead && fRd.BaseStream.Position < fRd.BaseStream.Length - 4 &&
                    MessageBox.Show("Initiate message reception", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                int msgSize = fRd.ReadInt32();
                byte[] msgArr = fRd.ReadBytes(msgSize);

                if (OnMessageReceived != null)
                    OnMessageReceived(msgArr);
            }
        }

        public override event CommLinkAbstract.MessageReceived OnMessageReceived;

        public override event CommLinkAbstract.ConnectionStatusChange OnConnectionStatusChange;

        /// <summary>
        /// Returns connection type name
        /// </summary>
        /// <returns>Connection type name</returns>
        public override string getName()
        {
            return "file";
        }
    }
}
